package com.example.service.impl;

import com.example.dao.RecordDao;
import com.example.domain.ExcelData;
import com.example.domain.Record;
import com.example.service.RecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordDao recordDao;

    @Override
    public List<Record> selectAllRecord() {
        return recordDao.selectAllRecord();
    }

    @Override
    public Record selectRecordById(Integer id) {
        return recordDao.selectRecordById(id);
    }

    @Override
    public List<Record> selectRecordByName(Record record) {
        return recordDao.selectRecordByName(record);
    }

    @Override
    public int addRecord(Record record) {
        return recordDao.addRecord(record);
    }

    @Override
    public int updateRecordByName(String name) {
        return recordDao.updateRecordByName(name);
    }

    @Override
    public int updateRecord(Record record) {
        return recordDao.updateRecord(record);
    }

    @Override
    public int deleteRecord(Integer id) {
        return recordDao.deleteRecord(id);
    }

    @Override
    public int deleteAll() {
        return recordDao.deleteAll();
    }

    @Override
    public PageInfo<Record> findPage(int page, int size) {
        // 开启分页插件,放在查询语句上面 帮助生成分页语句
        PageHelper.startPage(page, size); //底层实现原理采用改写语句   将下面的方法中的sql语句获取到然后做个拼接 limit AOP技术
        List<Record> listUser = recordDao.selectAllRecord();
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类
        return new PageInfo<Record>(listUser);
    }
    /**
     * 结果的导出
     */
    @Override
    public ExcelData getOutputExcelProjectData() {


        //Excel实体类
        ExcelData excelData = new ExcelData();

        //找到表中需要添加的数据
        List<Record> list = recordDao.selectAllRecord();

        //设置sheet名称
        excelData.setName("考勤信息");

        //设置表头字段
        List<String> titles = new ArrayList<String>();
        titles.add("Id");
        titles.add("员工姓名");
        titles.add("早上打卡");
        titles.add("下午打卡");
        titles.add("状态");
        excelData.setTitles(titles);

        //把expertUserList转换为List<List<Object>> rows
        List<List<Object>> rows = new ArrayList<List<Object>>();

        //加入数据
        for (Record record : list) {
            List<Object> row = new ArrayList<Object>();
            row.add(record.getId());
            row.add(record.getName());
            row.add(record.getSatime());
            row.add(record.getEntime());
            row.add(record.getStatus());
            rows.add(row);
        }

        //设置要输出的记录
        excelData.setRows(rows);
        return excelData;
    }
}
